﻿using System.Collections.Generic;
using System.Linq;
using QASPRibbon.Extensions;
using Schema = QASPRibbon.RibbonSchema;

namespace QASPRibbon.Controls
{
    public class RibbonGroupSection : RibbonControl
    {
        internal override string XmlTag
        {
            get { return Schema.GroupSection; }
        }

        public static RibbonGroupSection Divider
        {
            get
            {
                return new RibbonGroupSection
                    {
                        Type = RibbonSectionType.Divider,
                        Controls = new List<RibbonControl>()
                    };
            }
        }

        public RibbonSectionType Type
        {
            get { return _type; }
            set { _type = value; }
        }

        /// <summary>
        /// An enumeration value that determines how controls in the group are rendered. The default is Image32AndCaption.
        /// </summary>
        public RibbonDisplayMode DisplayMode
        {
            get { return _displayMode; }
            set { _displayMode = value; }
        }

        /// <summary>
        /// A collection of child controls.
        /// </summary>
        public IEnumerable<RibbonControl> Controls { get; set; }
        
        /// <summary>
        ///  An enumeration value that specifies the vertical alignment of controls within the group. The default value is Top.
        /// </summary>
        public RibbonSectionAlignment Alignment
        {
            get { return _alignment; }
            set { _alignment = value; }
        }

        internal IEnumerable<RibbonControl> AllControls
        {
            get
            {
                var res = new List<RibbonControl>();
                var controlIndex = 0;
                foreach (var control in Controls.Where(c => c.Visible))
                {
                    controlIndex++;
                    if (!control.FullId.Contains(".Controls."))
                        control.NameSpace = FullId + ".Controls";
                    control.Sequence = controlIndex;
                    if (control is IRibbonContainer<RibbonControl>)
                    {
                        var container = control is IRibbonMenu
                                            ? ((IRibbonMenu)control)
                                            : ((IRibbonContainer<RibbonControl>)control);
                        res.Add(control);
                        res.AddRange(container.AllControls());
                    }
                    else if (control is RibbonStrip)
                    {
                        res.AddRange(((RibbonStrip)control).AllControls);
                    }
                    else
                    {
                        res.Add(control);
                    }
                }
                return res;
            }
        }

        private RibbonSectionType _type = RibbonSectionType.OneRow;
        private RibbonDisplayMode _displayMode = RibbonDisplayMode.Image32AndCaption;
        private RibbonSectionAlignment _alignment = RibbonSectionAlignment.Top;
    }
}
